<?php

namespace app\service\shopify\action\graph;

class OrderGraph extends GraphQLBase
{

    public static $draftOrderResult = <<<DRAFT
draftOrder {
      id
      name
      status
      currencyCode
      completedAt
      ready
      subtotalPriceSet {
        presentmentMoney {
          amount
          currencyCode
        }
        shopMoney {
          amount
          currencyCode
        }
      }
      totalDiscountsSet {
        presentmentMoney {
          amount
          currencyCode
        }
        shopMoney {
          amount
          currencyCode
        }
      }
      totalPriceSet {
        presentmentMoney {
          amount
          currencyCode
        }
        shopMoney {
          amount
          currencyCode
        }
      }
      totalShippingPriceSet {
        presentmentMoney {
          amount
          currencyCode
        }
        shopMoney {
          amount
          currencyCode
        }
      }
       totalTaxSet {
        presentmentMoney {
          amount
          currencyCode
        }
        shopMoney {
          amount
          currencyCode
        }
      }
      order {
        id
        statusPageUrl
        name
      }
    }
    userErrors {
      field
      message
    }
DRAFT;


    /**
     * @param array $input
     * @return mixed
     * @throws \Exception
     * @desc 创建草稿订单
     * @doc https://shopify.dev/docs/api/admin-graphql/latest/mutations/draftordercreate
     */
    public function create_draft_order(array $input = [])
    {
        $query = <<<QUERY
mutation draftOrderCreate(\$input: DraftOrderInput!) {
  draftOrderCreate(input: \$input) {
    %s
  }
}
QUERY;
        $query = sprintf($query, self::$draftOrderResult);
        return $this->post($query, ['input' => $input]);
    }

    /**
     * @param string $id
     * @param array $input
     * @return mixed
     * @throws \Exception
     * @desc 更新草稿订单
     * @doc https://shopify.dev/docs/api/admin-graphql/latest/mutations/draftorderupdate
     */
    public function update_draft_order(string $id, array $input = [])
    {
        $query = <<<QUERY
mutation draftOrderUpdate(\$id: ID!, \$input: DraftOrderInput!) {
  draftOrderUpdate(id: \$id, input: \$input) {
     %s
   }  
}
QUERY;
        $query = sprintf($query, self::$draftOrderResult);
        return $this->post($query, ['id' => $id, 'input' => $input]);
    }

    /**
     * @param string $id
     * @return mixed
     * @throws \Exception
     * @desc 完成草稿订单
     * @doc https://shopify.dev/docs/api/admin-graphql/latest/mutations/draftordercomplete
     */
    public function complete_draft_order(string $id)
    {
        $query = <<<QUERY
mutation draftOrderComplete(\$id: ID!) {
  draftOrderComplete(id: \$id) {
   %s
  }
}
QUERY;
        $query = sprintf($query, self::$draftOrderResult);
        return $this->post($query, ['id' => $id]);
    }

    public function retrieve_a_specific_order(string $shopify_order_id)
    {
        $query = <<<QUERY
query order(\$shopify_order_id: ID!) {
  order(id: \$shopify_order_id) {
       id
       name
       statusPageUrl
 }
}
QUERY;
    return $this->post($query, ['shopify_order_id' => $shopify_order_id]);
    }

    public function retrieve_a_draft_order(string $draft_id)
    {
        $query = <<<QUERY
query draftOrder(\$draft_id: ID!) {
  draftOrder(id: \$draft_id) {
       ready
       order{
         statusPageUrl
       }
 }
}
QUERY;
        return $this->post($query, ['draft_id' => $draft_id]);
    }

}